//
// Created on 2024/4/29.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".

#ifndef DIANPING_CAPI_DEMO_WATERFLOWVIEWCOMPONENTINSTANCE_H
#define DIANPING_CAPI_DEMO_WATERFLOWVIEWCOMPONENTINSTANCE_H
#include <stack>
#include "ComponentInstance.h"
#include "nodes/WaterFlowNode.h"
#include "components/ComponentInstance.h"
#include "nodes/LoadingProgressNode.h"
#include "nodes/NodeAdapter.h"
#include "nodes/RefreshNode.h"
#include "nodes/StackNode.h"
#include "nodes/ScrollNode.h"
#include "nodes/WaterFlowNode.h"
#include "common/DataSource.h"
#include "components/FlowItemViewComponentInstance.h"
#include "Model/WaterFlowViewModel.h"

class WaterFlowViewComponentInstance : public ComponentInstance, public WaterFlowNodeScrollDelegate, public RefreshNodeDelegate, public DataChangeListener {
private:
    StackNode m_stackRootContainerNode;
    RefreshNode m_refreshNode;
    ScrollNode m_scrollNode;
    LoadingProgressNode m_loadingProgressNode;
    WaterFlowNode m_waterFlowNode;
//     FlowItemViewComponentInstance m_flowItemViewHeaderNode;
    StackNode m_stackSubContainerNode;

    // m_data主要是用于辅助WaterFlowItem来构建的
    std::vector<std::string> m_data;
    // 节点缓存池，用于回收复用。
    std::stack<ArkUI_NodeHandle> m_nodePool;
    std::stack<std::shared_ptr<ComponentInstance>> m_viewPool;
    // m_nodeAdapter 用于主动触发节点的创建、更新、删除和reload等操作
    SharedNodeAdapter m_nodeAdapter;
    std::shared_ptr<WaterFlowViewModel> m_waterFlowViewModel;
//     std::vector<FlowItemViewComponentInstance> m_flowItemViewNodeList;
    
    std::unordered_map<ArkUI_NodeHandle, std::shared_ptr<ComponentInstance>> nodeViewMap;

public:
    WaterFlowViewComponentInstance(Context context);
    ~WaterFlowViewComponentInstance() {
        OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "WaterFlowViewComponentInstance", 
            "~WaterFlowViewComponentInstance 0x%{public}x", this);
    }
    virtual void setModel(std::shared_ptr<ViewModel> model) override {
        m_waterFlowViewModel = std::dynamic_pointer_cast<WaterFlowViewModel>(model);
        build();
    }
    
    void build() override;

    void onChildInserted(ComponentInstance::Shared const &childComponentInstance, std::size_t index) override;
    void onChildRemoved(ComponentInstance::Shared const &childComponentInstance) override;
    
    // WaterFlowNodeDelegate implementation
    void onScroll() override;
    void onScrollStart() override;
    void onScrollStop() override;
    float onScrollFrameBegin(float offset, int32_t scrollState) override;

    // RefreshNodeDelegate implementation
    void onRefresh() override;
    void onStateChange() override;

    // DataChangeListener implementation
    void onAttachToNode(int32_t index) override;
    void onDetachToNode(int32_t index) override;
    void onSetChildId(SharedNodeAdapterEvent sharedNodeAdapterEvent) override;
    void onCreateChild(SharedNodeAdapterEvent sharedNodeAdapterEvent) override;
    void onDisposeChild(ArkUI_NodeHandle node) override;

    StackNode &getLocalRootArkUINode() override;
};

#endif //DIANPING_CAPI_DEMO_WATERFLOWVIEWCOMPONENTINSTANCE_H
